package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.api.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.bean.ListStore;
import com.epfresh.bean.MarketBean;
import com.epfresh.bean.TypeBean;
import com.epfresh.views.FlexibleRoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class StoreGridAdapter extends BaseAdapter {

    public Context context;
    private List<ArrayList<ListStore>> list;
    private View.OnClickListener onItemClickListener;

    public StoreGridAdapter(Context context, ArrayList<ArrayList<ListStore>> list) {
        this.context = context;
        this.list = list;
    }

    public View.OnClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderStores vhStores = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_newstore_copy, null, false);
            vhStores = new ViewHolderStores();
            vhStores.ll_store1 = convertView.findViewById(R.id.ll_store1);
            vhStores.stores[0] = convertView.findViewById(R.id.v_1);
            vhStores.stores[1] = convertView.findViewById(R.id.v_2);

            for(int i=0;i<vhStores.storesNumber;i++){
                vhStores.tv_store_name[i] = (TextView) vhStores.stores[i].findViewById(R.id.tv_store_name);
                vhStores.tv_store_number[i] = (TextView) vhStores.stores[i].findViewById(R.id.tv_store_number);
                vhStores.tv_goods_number[i] = (TextView) vhStores.stores[i].findViewById(R.id.tv_goods_number);
                vhStores.iv_phone[i] = vhStores.stores[i].findViewById(R.id.iv_phone);
                vhStores.iv_flag[i] = (ImageView) vhStores.stores[i].findViewById(R.id.iv_flag);
                vhStores.iv_store[i] = (ImageView) vhStores.stores[i].findViewById(R.id.iv_store);
                vhStores.stores[i].setOnClickListener(onItemClickListener);
                vhStores.iv_phone[i].setOnClickListener(onItemClickListener);
            }
            convertView.setTag(vhStores);
        } else {
            vhStores = (ViewHolderStores) convertView.getTag();
        }
        ArrayList<ListStore> flag = list.get(position);

        for(int i=0;i<vhStores.storesNumber;i++){
            vhStores.stores[i].setVisibility(View.INVISIBLE);
        }
        for (int i = 0; i < flag.size()&&i<vhStores.storesNumber; i++) {
            ListStore storesBean= flag.get(i);

            vhStores.stores[i].setTag(R.id.item_key_position, 10*position+i);
            vhStores.iv_phone[i].setTag(R.id.item_key_position, 10*position+i);

            vhStores.stores[i].setVisibility(View.VISIBLE);
            vhStores.tv_store_name[i].setText(storesBean.getName());
            if(storesBean.getStall()!=null) {
                vhStores.tv_store_number[i].setText(storesBean.getStall());
            }else{
                vhStores.tv_store_number[i].setText("");
            }
            vhStores.tv_goods_number[i].setText("共"+storesBean.getOnShelvesProductCount()+"件商品");
            vhStores.iv_flag[i].setVisibility(View.GONE);
            //ImageLoader.getInstance().displayImage(storesBean.getLogoUrl(), new ImageViewAware(vhStores.iv_store[i]));
            setRoundedImage(storesBean.getHeaderImage(), 4, FlexibleRoundedBitmapDisplayer.CORNER_TOP_LEFT|FlexibleRoundedBitmapDisplayer.CORNER_TOP_RIGHT,
                    R.mipmap.store_head_defaut,
                    vhStores.iv_store[i]
            );
            if(storesBean.getIsAcceptOrders()!=null&&storesBean.getIsAcceptOrders()==1){
                if(storesBean.getRecType()!=null&&storesBean.getRecType()==1){
                    vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                    vhStores.iv_flag[i].setImageResource(R.mipmap.store_recent_triangle);
                }else if(storesBean.getRecType()!=null&&storesBean.getRecType()==2) {
                    vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                    vhStores.iv_flag[i].setImageResource(R.mipmap.store_concern_triangle);
                }
            }else{
                vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                vhStores.iv_flag[i].setImageResource(R.mipmap.store_sleep_triangle);
            }
        }

        return convertView;

    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
    public class ViewHolderStores {
        int storesNumber = 2;
        View ll_store1;//, ll_store2;
        View[] stores = new View[storesNumber];//v_1, v_2, v_3, v_4, v_5, v_6;
        TextView[] tv_store_name = new TextView[storesNumber];
        TextView[] tv_store_number = new TextView[storesNumber];
        TextView[] tv_goods_number = new TextView[storesNumber];
        ImageView[] iv_flag = new ImageView[storesNumber];
        ImageView[] iv_store = new ImageView[storesNumber];
        View[] iv_phone = new ImageView[storesNumber];
    }


    /*
     * 设置图片---自定义图片4个角中的指定角为圆角
     * @param url 图片的url
     * @param cornerRadius 圆角像素大小
     * @param corners 自定义圆角:<br>
     * 以下参数为FlexibleRoundedBitmapDisplayer中静态变量:<br>
     * CORNER_NONE　无圆角<br>
     * CORNER_ALL 全为圆角<br>
     * CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT　指定圆角（选其中若干组合  ） <br>
     * @param image url为空时加载该图片
     * @param imageView 要设置图片的ImageView
     */

    public void setRoundedImage(String url, int cornerRadius, int corners, int image, ImageView imageView) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(image).showStubImage(image)
                .showImageForEmptyUri(image)//url为空时显示的图片
                .showImageOnFail(image)//加载失败显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new FlexibleRoundedBitmapDisplayer(cornerRadius,corners)) // 自定义增强型BitmapDisplayer
                .build();
                 imageLoader.displayImage(url, imageView, options);
    }

    public static DisplayImageOptions getTopRoundOptions(String expectTag) {
        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.store_logo) // 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.store_logo)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.store_logo) // 设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                .displayer(new FadeInBitmapDisplayer(300, true, false, false))// 是否图片加载好后渐入的动画时间
                .setExceptTag(expectTag)
                .build();// 构建完成
    }

}
